package io.renren.controller;

import io.renren.constant.AppConstant;
import io.renren.entity.*;
import io.renren.service.NewsBulletinService;
import io.renren.vo.Pager;
import io.renren.vo.RespModel;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news/bulletin")
@Api(value="/newsBulletin",tags="新闻公告")
@Component
public class NewsBulletinResourceController {

	private static final Logger logger  = LoggerFactory.getLogger(NewsBulletinResourceController.class);
    @Autowired
	private NewsBulletinService newsBulletinService;
	
	@GetMapping(value = "/list/bulletinType",produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "查询新闻公告类型", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {
	@ApiResponse(code = 200, message = "OK", response = RespModel.class),
	@ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
//	@com.cxytiandi.encrypt.springboot.annotation.Decrypt
	@com.cxytiandi.encrypt.springboot.annotation.Encrypt
	public RespModel<List<News>> queryBulletinTypeList(){
		List<News> list = newsBulletinService.listChannelByParent(0);
		logger.info("查询新闻公告类型..............");
		RespModel<List<News>> resp = new RespModel<>(AppConstant.STATU_SUCCESS.getName(),AppConstant.STATU_SUCCESS.getValue(),"",list);
		return resp;
	}
	
	@PostMapping(value = "/list/{bulletinType}",produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "通过公告类型查询公告集合(交通公告、办事指南、交警动态)", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {
	@ApiResponse(code = 200, message = "OK", response = RespModel.class),
	@ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
//	@com.cxytiandi.encrypt.springboot.annotation.Decrypt
	@com.cxytiandi.encrypt.springboot.annotation.Encrypt
	public RespModel<Pager<Topic>> queryBulletinByType(@ApiParam(value = "公告类型") @PathVariable int bulletinType,
                                                       @ApiParam(value = "页号") @RequestParam("page") int page,
                                                       @ApiParam(value = "分页大小") @RequestParam("limit") int limit){
		Pager<Topic> pager = newsBulletinService.queryTopic(bulletinType, page, limit);
		logger.info("通过公告类型查询公告集合(交通公告、办事指南、交警动态)..............入参bulletinType="+bulletinType);
		RespModel<Pager<Topic>> resp = new RespModel<>(AppConstant.STATU_SUCCESS.getName(),AppConstant.STATU_SUCCESS.getValue(),"",pager);
		return resp;
	}
	
	@GetMapping(value = "/get/{id}", produces = MediaType.TEXT_HTML_VALUE)
	@ApiOperation(value = "通过公告ID查询公告详细内容(交通公告、办事指南、交警动态)", produces = MediaType.TEXT_HTML_VALUE)
	@ApiResponses(value = {
	@ApiResponse(code = 200, message = "OK"),
	@ApiResponse(code = 500, message = "服务端异常")})
//	@com.cxytiandi.encrypt.springboot.annotation.Decrypt
	@com.cxytiandi.encrypt.springboot.annotation.Encrypt
	public @ResponseBody String queryBulletinById(@ApiParam(value = "公告ID") @PathVariable int id){
		logger.info("通过公告ID查询公告详细内容(交通公告、办事指南、交警动态)..............入参ID="+id);
		Topic topic = newsBulletinService.load(id);
		if(topic != null) {
            return "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body>" + topic.getContent() + "</body></html>";
        }else{
            return "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><h1>内容不存在!</h1></body></html>";
        }
	}
	
	@GetMapping(value = "/list/img",produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "获取首页上方广告图片集合", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {
	@ApiResponse(code = 200, message = "OK", response = RespModel.class),
	@ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
//	@com.cxytiandi.encrypt.springboot.annotation.Decrypt
	@com.cxytiandi.encrypt.springboot.annotation.Encrypt
	public RespModel<List<Attachment>> queryImageList(){
		List<Attachment> list = newsBulletinService.listByTopic(20);
		logger.info("获取首页上方广告图片集合..............");
		RespModel<List<Attachment>> resp = new RespModel(AppConstant.STATU_SUCCESS.getName(),AppConstant.STATU_SUCCESS.getValue(),"",list);
		return resp;
	}
}
